
<?php
$hsd = 2592000;
$this->headTitle('Chọn Thiết Bị','append');
?>

<script type="text/javascript">
$(document).ready(function() {
    $('input[type=checkbox]').click( function() {
        $(this).attr('value',this.checked);
        $(this).attr('check',this.checked);
    });
    $('.tabs-menu li').click(function(){
        id = $(this).attr('id');
        $(this).parent().find('li').removeClass('active');
        $(this).addClass('active');
        $(this).parent().parent().find('div.tabs').addClass('hidden');
        $(this).parent().parent().find('.tabs.'+id).removeClass('hidden');
    });
    $('select[name="categoryId[]"]').parent().find('option[value=1]').before('<option value="1-5" <?php if ($this->category && in_array("1-5",$this->category)) echo 'selected="true"';?>>Server all</option>');
});

</script>
<div class="grid grid_8" style="margin-top: 20px;">
    <div id="tabs-search">
      <ul class="tabs-menu">
        <li class="active" id="tabs-1"><span>Tìm kiếm thiết bị</span></li>
        <li id="tabs-2"><span>Server Free</span></li>
      </ul>
      <div class="tabs tabs-1">
            <form class='frm-search frm-search-equipment' name='frm-search' action='/equipment/search/mul/1/id/<?php echo $this->itemId;?>' method="get" enctype="application/x-www-form-urlencoded">
                <div class='search-option-wrapper'>	
                	<div style="clear: both"></div>
                	<ul class='search-option-select'>
                        <li class="first keyword">
                            <label>Từ khóa</label>
                            <input value='<?php echo $this->keyword; ?>' placeholder="<?php echo $this->keyword ? $this->keyword : 'Từ khóa ...';?>" name='keyword'/>
                        </li>
                		<li class="first">
                			<label>Nhóm</label>
                			<select class="multiple" name="categoryId[]" multiple="multiple">
                				<option value="0">Tất cả</option>
                				<?php foreach ($this->sortCate as $key=>$category){ ?>
                					<option value ="<?php echo $category['categoryId']; ?>" <?php if ($this->category && in_array($category['categoryId'],$this->category)) echo "selected='true'";?>><?php echo $category['categoryName']; ?></option>
                				<?php } ?>				
                			</select>			
                		</li>
                		<li>		
                			<label>Dự án</label>
                			<select class="multiple" name="projectId[]" multiple="multiple">
                				<option value="0" >Tất cả</option>
                				<?php foreach ($this->sortProject as $key=>$project) { ?>
                					<option value="<?php echo $project['projectId']; ?>" <?php if($this->project && in_array($project['projectId'], $this->project)) echo "selected='true'"; ?>><?php echo $project['projectName']; ?></option>
                				<?php } ?>				
                			</select>				
                		</li>
                		<li>			
                			<label>Tài sản</label>
                			<select class="multiple" name="ownerId[]" multiple="multiple">
                				<option value="" >Tất cả</option>
                				<?php foreach ($this->sortOwner  as $key=>$owner) { ?>
                					<option value="<?php echo $owner['owner']; ?>" <?php if($this->owner && in_array($owner['owner'], $this->owner)) echo "selected='true'"; ?>><?php echo $owner['owner']; ?></option>
                				<?php } ?>				
                			</select>			
                		</li>	
                		<li>
                			<label>Thiết bị dùng chung</label>
                			<select name="isGeneral">
                				<option value="-1"<?php if($this->general == -1 ) echo "selected='true'"; ?>>--Không Chọn--</option>
                				<option value="0" <?php if($this->general == 0 ) echo "selected='true'"; ?>>Không</option>
                				<option value="1" <?php if($this->general == 1 ) echo "selected='true'"; ?> >Có</option>
                			</select>						
                		</li>
                        <li class="button">
                            <input type="submit" name="btn-search" class="submit" value="Tìm kiếm" />
                        </li>
                	   <div style="clear: both"></div>
                	</ul>
                	<div style="clear: both"></div>
                </div>
            </form>
      </div>
      <div class="tabs tabs-2 hidden">
            <form class='frm-search frm-search-equipment' name='frm-search-serverfree' action='/equipment/search/mul/1/id/<?php echo $this->itemId;?>' method="get">
                <div class='search-option-wrapper'>	
                	<div style="clear: both"></div>
                	<ul class='search-option-select'>
                		<li class="first">
                			<label>Dự án</label>
                        </li>
            			<li>
            				<label>wingame-free</label>
            				<input class="checkbox" type="checkbox" name="project_34" value="34" />
            			</li>
            			<li>
            				<label>ssgame-free</label>
            				<input class="checkbox" type="checkbox" name="project_38" value="38" />
            			</li>
            			<li>
            				<label>ssgroup-free</label>
            				<input class="checkbox" type="checkbox" name="project_47" value="47" />
            			</li>
                        <li class="button">
                            <input type="submit" name="btn-search" class="submit" value="Tìm kiếm" />
                        </li>
                	   <div style="clear: both"></div>
                	</ul>
                	<div style="clear: both"></div>
                </div>
            </form>
      </div>
    </div>
</div>
<div class="grid grid_8" style="margin-top: 20px;">
    <?php if($this->equipment_list){ ?>
    <div class="box table">
        <table cellspacing="0" class="content_equipment">
        	<thead id="detail">
        		<tr>
        			<td>Mã Thiết Bị</td>
        			<?php
            			
                        //equipment
                        if(in_array('code',$this->arr_name))
                            echo  '<td>Mã quản lý</td>';
                        if(in_array('equipmentName',$this->arr_name))
                            echo  '<td>Tên thiết bị</td>';
                        if(in_array('function',$this->arr_name))
                            echo  '<td>Chức năng</td>';
                        if(in_array('endUser',$this->arr_name))
                            echo  '<td>Người sử dụng</td>';
                        if(in_array('username',$this->arr_name))
                            echo  '<td>Quản lý kỹ thuật</td>';     
                        if(in_array('categoryName',$this->arr_name))
                            echo  '<td>Nhóm</td>';
                        if(in_array('projectName',$this->arr_name))
                            echo  '<td>Dự án</td>';
                        //item
                        if(in_array('holders',$this->arr_name))
                            echo  '<td>Người ký hợp đồng</td>';
                        if(in_array('startDate',$this->arr_name))
                            echo  '<td>Ngày bắt đầu</td>';
                        if(in_array('duration',$this->arr_name))
                            echo  '<td>Ngày kết thúc</td>'; 
                        if(in_array('owner',$this->arr_name))
                            echo  '<td>Tài sản</td>';
                        if(in_array('borrow',$this->arr_name))
                            echo  '<td>Công ty đang sử dụng</td>';
                       
        			?>
        			<td>Tuỳ chọn</td>
        		</tr>
        	</thead>
        	<tbody>
            <?php            
                $temp = 'odd';
                foreach($this->equipment_list as $equipment){ 
            ?>
                <tr id="row<?php echo $equipment['equipmentId']; ?>" class="row-<?php echo $temp;?>"<?php if($equipment['trackDate'] > 0 && $equipment['trackDate'] <= date("Y-m-d")) echo "style='background:#cef6ff'";elseif($equipment['isGeneral'] == 1) echo "style='background:#FFF8C6'"; ?>>
                    <?php
                        //equipment
                       if(isset($equipment['equipmentId']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['equipmentId']),'">',
                                    $equipment['equipmentId'] ? $this->stringTrim(str_replace("\r\n","",$equipment['equipmentId']), 40) : '-','
                                    </span>
                                </td>';  
                        if(isset($equipment['code']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['code']),'">',
                                    $equipment['code'] ? $this->stringTrim(str_replace("\r\n","",$equipment['code']), 40) : '-','
                                    </span>
                                </td>';
                        if(isset($equipment['equipmentName']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['equipmentName']),'">',
                                    $equipment['equipmentName'] ? $this->stringTrim(str_replace("\r\n","",$equipment['equipmentName']), 40) : '-','
                                    </span>
                                </td>';
                        
                        if(isset($equipment['function']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['function']),'">',
                                    $equipment['function'] ? $this->stringTrim(str_replace("\r\n","",$equipment['function']), 40) : '-','
                                    </span>
                                </td>';
                        if(isset($equipment['endUser']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['endUser']),'">',
                                    $equipment['endUser'] ? $this->stringTrim(str_replace("\r\n","",$equipment['endUser']), 40) : '-','
                                    </span>
                                </td>';
                        if(in_array('username',$this->arr_name))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['username']),'">',
                                    $equipment['username'] ? $this->stringTrim(str_replace("\r\n","",$equipment['username']), 40) : '-','
                                    </span>
                                </td>';
                        if(in_array('categoryName',$this->arr_name))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['categoryName']),'">',
                                    $equipment['categoryName'] ? $this->stringTrim(str_replace("\r\n","",$equipment['categoryName']), 40) : '-','
                                    </span>
                                </td>';
                        if(in_array('projectName',$this->arr_name)){
                            echo '<td>';
                            $stt = 1;
                            foreach($equipment['project_list'] as $project){
                                if($stt == 1)
                                    echo '"',$this->stringTrim(str_replace("\r\n","",$project["projectName"]), 40),'"';
                                else
                                    echo ' - "',$this->stringTrim(str_replace("\r\n","",$project["projectName"]), 40),'"';
                                ++$stt;
                            }
                            echo '</td>';
                        }        
                        //item
                        if(in_array('holders',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',str_replace('"',"'",$item['holders']),'">',
                                            $item['holders'] ? $this->stringTrim(str_replace("\r\n","",$item['holders']), 9) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('startDate',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',$item['startDate'] ? $this->time()->dataToDate($item['startDate']) : '-','">',
                                            $item['startDate'] ? $this->time()->dataToDate($item['startDate']) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('duration',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',$item['duration'] ? $this->time()->dataToDate($item['duration']) : '-','">',
                                            $item['duration'] ? $this->time()->dataToDate($item['duration']) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                      
                        if(in_array('owner',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',str_replace('"',"'",$item['owner']),'">',
                                            $item['owner'] ? $this->stringTrim(str_replace("\r\n","",$item['owner']), 9) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('borrow',$this->arr_name))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['borrow']),'">',
                                    $equipment['borrow'] ? $this->stringTrim(str_replace("\r\n","",$equipment['borrow']), 40) : '-','
                                    </span>
                                </td>';
                    ?> 
    			<td class="tc">
    				<div class="addToEquipment">
                        <img src="image/icons/small/icon_server1_(add)_16x16.gif" title="Add" onclick="add(<?php echo $equipment['equipmentId'];?>);"/>
                        <input type="hidden" class="name<?php echo $equipment['equipmentId'];?>" value="<?php echo $equipment['equipmentName'];?>"/>
                    </div>
                   
    				
    			</td>
            </tr>
            <?php 
                    if($temp == 'odd')
                        $temp = 'even';
                    else
                        $temp = 'odd';
                } 
            ?>
             </tbody>
             <!-- BEGIN: PAGINATION (FOOT) -->
             <tfoot>
        		<tr>
        			<td colspan="<?php echo count($this->arr_header)+2?>">
        				<div class="container">
        					<div class="pagination">
        						<?php
        							echo $this->paginationControl($this->panigator,'Sliding','paginator.phtml', array('sel' => 'search', 'keyword' => $this->keyword, 'projectId' => $this->project, 'categoryId' => $this->category, 'ownerId' => $this->owner, 'isGeneral' => $this->general));
        						?>
        					</div>
        				</div>
        			</td>
        		</tr>
        	  </tfoot>
        	  <!-- END: PAGINATION (FOOT) -->
        </table>
    </div>
    <?php }elseif($this->search == 0){ ?>
    
    <?php }else{ ?>
    <div class="notification error"><span class="strong">Chưa có dữ liệu</span></div>
    <?php } ?>
</div>
<div class="grid">
    <div class="box-header">
        Hướng dẫn
    </div>
    <div class="box ">
        - Thiết bị có <span class="red">màu đỏ</span> là những item sắp (đã) chuyển qua giai đoạn thuê chỗ.<br />
        <div class="bg-yellow">- Những dòng có nền màu vàng nhạt là những thiết bị dùng chung.</div>
        <div class="bg-green">- Những dòng có nền màu xanh nhạt là những thiết bị được theo dõi (sẽ được cập nhật trong thời gian tới).</div>
    </div>
</div>
<script>
	var w = $(".content_equipment").width();
	$(".grid .box.table").css('width', w);
</script>
<script>
         function add(itemEquipmentId){
            var name=$('.name'.concat(itemEquipmentId)).val();
            var e=confirm('Bạn có muốn gắn vào thiết bị '+name+' không?');
            if(e){
                var itemId=<?php echo $this->itemId;?>;
                $.ajax({
                type:     "GET",
                url:      "http://local.cmdb.ssgroup.com.vn/default/item/freedom/id/"+itemId+"/equi/"+itemEquipmentId+"/",
                });
                alert('Đã gắn vào thiết bị '+name);
                //set thoi gian dong fancy box
                setTimeout("parent.$.fancybox.close()", 1000);
                //f5 trang
                top.location= 'http://local.cmdb.ssgroup.com.vn/item/list-free'; 
                }
            else{
                return false;
                }   
            
         }
</script>    
